package arrary;

/**
 * @author zhaoziqiang
 * @Description: 放心数组
 * @date 2020/8/12 16:33
 */

public class Array<E> {
    private E[] data;
    private int size;

    // 构造函数，传入数组的容量capacity构造Array
    public Array(int capacity) {
        this.data = (E[])new Object[capacity];
        this.size = 0;
    }

    //获取最后元素
    public E getLast() {
        return get(size - 1);
    }

    //获取第一个元素
    public E getFirst() {
        return get(0);
    }

    // 无参数的构造函数，默认数组的容量 capacity=10
    public Array() {
        this(10);
    }

    //数组元素个数
    public int getSize() {
        return this.size;
    }

    //数组容量
    public int getCapacity() {
        return data.length;
    }

    //数组是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    //在所有元素后添添加一个元素
    public void addLast(E e) {
        add(size, e);
    }

    //在所有元素前添加一个元素
    public void addFirst(E e) {
        add(0, e);
    }

    //任意index位置插入元素e
    public void add(int index, E e) {
        if (index < 0 || index > size)//连续插入
        {
            throw new IllegalArgumentException("add  failed.Require index<=0 and index<=size");
        }
        if (size == getCapacity())
            resize(data.length * 2);
        for (int i = size - 1; i >= index; i--) {
            data[i + 1] = data[i];
        }
        data[index] = e;
        size++;
    }

    private void resize(int newCapacity) {
        E[] newData = (E[])new Object[newCapacity];
        for (int i = 0; i <size; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }

    //获取index位置的元素
    public E get(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("get  failed");
        return data[index];
    }

    //修改index位置的元素
    public void set(int index, E e) {
        if (index < 0 || index > size)
            throw new IllegalArgumentException("set  failed");
        data[index] = e;
    }

    //数组中是否包含某个元素
    public boolean contains(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i] == e)
                return true;
        }
        return false;
    }

    //查找某个元素的索引
    public int find(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i] == e)
                return i;
        }
        return -1;
    }

    //删除index位置元素，并返回被删除元素
    public E remove(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("remove  failed.Index is out of arrange");
        E temp = data[index];
        for (int i = index; i < size-1; i++) {
            data[i] = data[i + 1];
        }
        data[size] = null;//loitery object
        size--;
        if (size == data.length / 4 && data.length / 2 != 0)
            resize(data.length / 2);// data.length ==0 不缩容
        return temp;
    }

    //删除第一个元素
    public E removeFirst() {
        return remove(0);
    }

    //删除最后一个元素
    public E removeLast() {
        return remove(size - 1);
    }

    //删除元素e boolean
    public void removeElement(E e) {
        if (find(e) != -1)
            remove(find(e));
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("Array:size=%d,capacity=%d\n", size, getCapacity()));
        sb.append("[");
        for (int i = 0; i < size; i++) {
            sb.append(data[i]);
            if (i != size - 1)
                sb.append(",");
        }
        sb.append("]");
        return sb.toString();
    }

}
